# coding=utf8

import os
import sys
import re

from openpyxl.reader.excel import load_workbook

reload(sys)
sys.setdefaultencoding('utf-8')

def get_y(line):
    matched = re.search( r'(\s*)(\[?)([^\[\]]+)(\]?)(\s*)', line, re.M|re.I)
    line = matched.group(3)
    line = line.replace(',', '')
    line = line.replace('\'', '')
    line = line.replace(':', '')
    return len(line)


def get_x(line):
    count = line.count(".") + 1
    return count


def process(work_book, course):
    word_sheet_name = "%s_word" % course
    print word_sheet_name
    wb = load_workbook(filename=work_book)
    in_ws = wb.get_sheet_by_name(word_sheet_name)
    out_ws = wb.get_sheet_by_name("%s_audio_time" % course)
    out_row = 2
    for r in range(2, in_ws.max_row + 1):
        symbol = str(in_ws.cell(row=r, column=3).value).strip()
        syllable = str(in_ws.cell(row=r, column=6).value).strip()
        x = get_y(symbol)
        y = get_x(syllable)
        out_ws.cell(row=out_row, column=2).value = 2*x + 2.97*y + 1.5
        out_row = out_row + 1
    
    wb.save(work_book)

